package com.centronix.web;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import com.centronix.bean.ComboBean;
import com.centronix.bean.PurchaseVO;
import com.centronix.domain.Product;
import com.centronix.domain.ProductType;
import com.centronix.manager.ProductCatManager;
import com.centronix.manager.ProductManager;
import com.centronix.manager.ProductTypeManager;
import com.centronix.manager.PurchasesManager;
import com.centronix.util.WebUtil;

@ManagedBean(name = "purchasesBean")
@SessionScoped
public class PurchasesBean implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	@ManagedProperty("#{purchasesManager}")
    private PurchasesManager purchasesManager;
	
	@ManagedProperty("#{productTypeManager}")
    private ProductTypeManager productTypeManager;
	
	@ManagedProperty("#{productCatManager}")
    private ProductCatManager productCatManager;
	
	@ManagedProperty("#{productManager}")
    private ProductManager productManager;
	
	private List<PurchaseVO> allPurchases = new ArrayList<PurchaseVO>(0);
	
	private List<ComboBean> productCatComboList =  new ArrayList<ComboBean>(0);
	private List<ComboBean> productTypeComboList =  new ArrayList<ComboBean>(0);
	private List<ComboBean> productComboList =  new ArrayList<ComboBean>(0);
	
	private PurchaseVO newPurchase = new PurchaseVO();
	private PurchaseVO selectedPurchase = new PurchaseVO();
	
	private boolean dataVisible = false;
	private String selectedProductCat = "";
	private String selectedProductType = "";
	private String selectedProduct = "";
	
	public String findAllPurchases(){
		allPurchases = purchasesManager.findAllPurchase();
		productCatComboList = productCatManager.getProductCatCombo();
		dataVisible = true;
		return "purchases";
	}
	
	public String loadProductTypeCMB(){
		int catId = Integer.valueOf(selectedProductCat);
		productTypeComboList = productTypeManager.getProductTypeCombo(catId);
		selectedProductType = "";
		selectedProduct = "";
		return "";
	}
	
	public String loadProductCMB(){
		int typeId = Integer.valueOf(selectedProductType);
		productComboList = productManager.getProductCombo(typeId);
		selectedProduct = "";
		return "";
	}
	
	public String saveNewPurchase(){
		int productId = Integer.valueOf(selectedProduct);
		newPurchase.setProductId(productId);
		purchasesManager.savePurchase(newPurchase);
		
		newPurchase = new PurchaseVO();
		selectedPurchase = new PurchaseVO();
		allPurchases = purchasesManager.findAllPurchase();
		
		productTypeComboList.clear();
		productComboList.clear();
		selectedProductCat = "";
		selectedProductType = "";
		selectedProduct = "";
		return "";
	}
	
	public String updatePurchase(){
		int productId = Integer.valueOf(selectedProduct);
		newPurchase.setProductId(productId);
		purchasesManager.savePurchase(selectedPurchase);
		
		newPurchase = new PurchaseVO();
		selectedPurchase = new PurchaseVO();
		allPurchases = purchasesManager.findAllPurchase();
		
		productTypeComboList.clear();
		productComboList.clear();
		selectedProductCat = "";
		selectedProductType = "";
		selectedProduct = "";
		return "";
	}
	
	public String deletePurchase(){
		purchasesManager.deletePurchase(selectedPurchase.getPurchaseId());
		allPurchases = purchasesManager.findAllPurchase();
		return "";
	}

	public PurchasesManager getPurchasesManager() {
		return purchasesManager;
	}

	public void setPurchasesManager(PurchasesManager purchasesManager) {
		this.purchasesManager = purchasesManager;
	}

	public List<PurchaseVO> getAllPurchases() {
		return allPurchases;
	}

	public void setAllPurchases(List<PurchaseVO> allPurchases) {
		this.allPurchases = allPurchases;
	}

	public boolean isDataVisible() {
		return dataVisible;
	}

	public void setDataVisible(boolean dataVisible) {
		this.dataVisible = dataVisible;
	}

	public ProductTypeManager getProductTypeManager() {
		return productTypeManager;
	}

	public void setProductTypeManager(ProductTypeManager productTypeManager) {
		this.productTypeManager = productTypeManager;
	}

	public ProductCatManager getProductCatManager() {
		return productCatManager;
	}

	public void setProductCatManager(ProductCatManager productCatManager) {
		this.productCatManager = productCatManager;
	}

	public ProductManager getProductManager() {
		return productManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	public List<ComboBean> getProductCatComboList() {
		return productCatComboList;
	}

	public void setProductCatComboList(List<ComboBean> productCatComboList) {
		this.productCatComboList = productCatComboList;
	}

	public List<ComboBean> getProductTypeComboList() {
		return productTypeComboList;
	}

	public void setProductTypeComboList(List<ComboBean> productTypeComboList) {
		this.productTypeComboList = productTypeComboList;
	}

	public List<ComboBean> getProductComboList() {
		return productComboList;
	}

	public void setProductComboList(List<ComboBean> productComboList) {
		this.productComboList = productComboList;
	}

	public PurchaseVO getNewPurchase() {
		return newPurchase;
	}

	public void setNewPurchase(PurchaseVO newPurchase) {
		this.newPurchase = newPurchase;
	}

	public PurchaseVO getSelectedPurchase() {
		return selectedPurchase;
	}

	public void setSelectedPurchase(PurchaseVO selectedPurchase) {
		this.selectedPurchase = selectedPurchase;
	}

	public String getSelectedProductCat() {
		return selectedProductCat;
	}

	public void setSelectedProductCat(String selectedProductCat) {
		this.selectedProductCat = selectedProductCat;
	}

	public String getSelectedProductType() {
		return selectedProductType;
	}

	public void setSelectedProductType(String selectedProductType) {
		this.selectedProductType = selectedProductType;
	}

	public String getSelectedProduct() {
		return selectedProduct;
	}

	public void setSelectedProduct(String selectedProduct) {
		this.selectedProduct = selectedProduct;
	}
	
	
}
